<?php

namespace app\admin\controller;

use app\admin\validate\Product;
use app\common\model\Comment;
use app\common\model\ProductImage;
use app\common\model\ProductStock;
use app\common\model\ProductsType;
use app\h5api\controller\Order;
use think\Db;

class Products extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('products/shopsproduct')
     * 商店商品列表
     */
    public function shopsproduct()
    {
        // 获取搜索条件
        $get['shop_name'] = input('get.shop_name');
        $get['state'] = input('get.state');
        $where = null;
        if($this->_shopid){
            $where[] = ['p.shop_id','=',$this->_shopid];
        }
//        判断用户类型
        $isdisplay = true;

        $isshop = $this->_shopid;
        if($isshop != 0){
            $isdisplay = false;
        }
        if(!empty($get['state'])){
            $where[] = ['p.state','=',$get['state']];
        }
        if(!empty($get['shop_name'])){
            $where[] = ['s.shop_name','like',"%".$get['shop_name']."%"];
        }
        $where[] = ['p.is_del','=',1];
        $where[] = ['p.class_id','=',1];
        // 后台账号数据
        $rs_data = Db::table('ddt_products')
            ->alias('p')
            ->leftJoin('ddt_shops s','s.shop_id = p.shop_id')
            ->leftjoin('ddt_products_type pt','pt.type_id = p.type_id')
            ->where($where)
            ->field('p.*,s.shop_name,pt.type_name')
            ->order('p.product_id desc')
            ->paginate($this->limit);

//        var_dump($subplatforms);die;
        $is_can_operate = false;
        if($this->_shopid == 0){
            $is_can_operate = true;
        }

        $tvar = array(
            "title" => "商品列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("is_can_operate",$is_can_operate);
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('products/shopsproduct');
    }

    /**
     * @route('products/boxeproduct')
     * 添加编辑商品
     */
    public function Boxeproduct()
    {

        $model = new \app\common\model\Products();
        $typeMdodel = new ProductsType();

        if(request()->isAjax()){

            $param = input();

            if($this->_shopid != 0){
                $approved = 1;
                $scene = 'EditProduct';
            } else {
                $approved = 2;
                $scene = 'HeadOfficeEditProduct';
            }
            $validate = new Product();
            $checkres = $validate->scene($scene)->check($param);
            if (!$checkres) {
                $this->error($validate->getError());
            }

//            $product_id = $this->regpost("product_id","int",0);
//            $type_id = $this->regpost("type_id","int");
//            $price = $this->regpost("price","float");
//            $mprice = $this->regpost("mprice","float");
//            $sprice = $this->regpost("sprice","float");

            $product_id = $param['product_id'];
            $type_id = $param['type_id'];
            $price = $param['price'];
            $mprice = $param['mprice'];
//            $sprice = $param['sprice'];

            $type = $typeMdodel->where('type_id','=',$type_id)->where('is_del','=',1)->find();

//            if($price < $mprice || $price < $sprice){
//                $this->error("商品原价不能小于会员价和销售价");
//            }
            if(empty($type)){
                $this->error("不存在该分类");
            }

            $data = [
//                "product_name" => $this->regpost("product_name","string"),
//                "imageurl" => $this->regpost("imageurl","string"),
//                "express_fee" => $this->regpost("express_fee","float"),
//                "state" => $this->regpost("status","int",1),
//                "is_online" => $this->regpost("is_online","int",1),
//                "describe" => $this->regpost("describe","html"),
//                "sort" => $this->regpost("sort","int"),
//
                "product_name" => $param['product_name'],
                "imageurl" => $param['imageurl'],
                "express_fee" => $param['express_fee'],
                "state" => $param['state'],
//                "is_online" => $param['is_online'],
                "integral" => $param['integral'],
                "jifen" => $param['jifen'],
                "describe" => $param['describe'],
                "sort" => $param['sort'],
                "show"  => $param['show'],
                "type_id" => $type_id,
                "price" => $price,
                "mprice" => $mprice,
//                "sprice" => $sprice,
                "admin_id" => $this->_userid,
                "shop_id" => $this->_shopid,
                "approved" => $approved,
                "salecount" => $param['salecount'],
            ];

//            var_dump($param);die;

            if($this->_shopid == 0 && isset($param['is_can_wholesale'])){
                $data['is_can_wholesale'] = $param['is_can_wholesale'];
            }
            if($product_id == 0){
                $rs = $model->edit($data);
            } else {
                $rs = $model->edit($data,$product_id);
            }
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/products/shopsproduct');
        }

        $field = 'type_id,p_id,type_name,sort';
        $cond['is_del'] = 1;
        $typelist = $typeMdodel->get_tree($field,$cond);
//        echo "<pre>";
//        var_dump($typelist);die;
        $product_id = $_GET['product_id'];
        $product = null;
        $tvar = array(
            "title" => "添加商品",
            "time"  => time(),
            "product_id"   => $product_id,
            "typelist" => $typelist,
        );
        $is_can_operate = false;
        if($this->_shopid == 0){
            $is_can_operate = true;
        }
        if($product_id != 0){
            $tvar["title"] = "编辑商品";
            $product = $model->where('product_id','=',$product_id)->find();
            if($product['shop_id'] != $this->_shopid){
                $is_can_operate = false;
            }
        }
//        echo "<pre>";
//        var_dump($tvar['typelist']);die;
        $this->assign("is_can_operate",$is_can_operate);
        $this->assign("data", $tvar);
        $this->assign("product", $product);
        return $this->fetch('products/boxeproduct');
    }

    /**
     * @route('products/exchangeproduct')
     * 兑换商品列表
     */
    public function exchangeproduct()
    {
        // 获取搜索条件
        $get['shop_name'] = input('get.shop_name');
        $get['state'] = input('get.state');

        $where = [
            'state' => $get['state'],
            'shop_name' => $get['shop_name'],
            'is_del' => 1,
            'class_id' => 2,
        ];
        $isdisplay = true;
        $isshop = $this->_shopid;
        if($isshop != 0){
            $where['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }
        $productsModel = new \app\common\model\Products();
        // 后台账号数据
        $rs_data = $productsModel->admin_get_list($where,'','p.*,s.shop_name,pt.type_name',$this->limit);

//        var_dump($rs_data);die;
        $tvar = array(
            "title" => "兑换商品列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('products/exchangeproducts/shopsproduct');
    }


    /**
     * @route('products/boxexchangeproduct')
     * 添加编辑兑换商品
     */
    public function BoxExchangeProduct()
    {

        $model = new \app\common\model\Products();
        $typeMdodel = new ProductsType();

        if(request()->isAjax()){

            $param = input();
//            var_dump(234);die;
            $validate = new Product();
            $checkres = $validate->scene('ExchangeProduct')->check($param);
//            var_dump($checkres);die;
            if (!$checkres) {
                $this->error($validate->getError());
            }

            $product_id = $param['product_id'];
            $type_id = $param['type_id'];

            $type = $typeMdodel->where('type_id','=',$type_id)->where('is_del','=',1)->find();

            if(empty($type)){
                $this->error("不存在该分类");
            }
            if($this->_shopid != 0){
                $approved = 1;
            } else {
                $approved = 2;
            }

            $param = [
//
                "product_name" => $param['product_name'],
                "imageurl" => $param['imageurl'],
                "express_fee" => $param['express_fee'],
                "state" => $param['state'],
                "integral" => $param['integral'],
//                "is_online" => $param['is_online'],
                "sort" => $param['sort'],

                "type_id" => $type_id,
                "admin_id" => $this->_userid,
                "shop_id" => $this->_shopid,
                "approved" => $approved,
                "class_id" => 2,
            ];
//            var_dump($param);die;

            if($product_id == 0){
                $rs = $model->edit($param);
            } else {
                $rs = $model->edit($param,$product_id);
            }
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/products/exchangeproduct');
        }

        $field = 'type_id,p_id,type_name,sort,add_time';
        $cond['is_del'] = 1;
        $typelist = $typeMdodel->get_tree($field,$cond);
//        echo "<pre>";
//        var_dump($typelist);die;
        $product_id = $_GET['product_id'];
        $product = null;
        $tvar = array(
            "title" => "添加兑换商品",
            "time"  => time(),
            "product_id"   => $product_id,
            "typelist" => $typelist,
        );
        if($product_id != 0){
            $tvar["title"] = "编辑兑换商品";
            $product = $model->where('product_id','=',$product_id)->find();
        }

        $this->assign("data", $tvar);
        $this->assign("product", $product);
        return $this->fetch('products/exchangeproducts/boxeproduct');
    }

    /**
     * @route('products/loveproduct')
     * 爱心餐商品列表
     */
    public function loveproduct()
    {
        // 获取搜索条件
        $get['shop_name'] = input('get.shop_name');
        $get['state'] = input('get.state');

        $where = [
            'state' => $get['state'],
            'shop_name' => $get['shop_name'],
            'is_del' => 1,
            'class_id' => 3,
        ];
        $isdisplay = true;
        $isshop = $this->_shopid;
        if($isshop != 0){
            $where['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }
        $productsModel = new \app\common\model\Products();
        // 后台账号数据
        $rs_data = $productsModel->admin_get_list($where,'','p.*,s.shop_name,pt.type_name',$this->limit);

//        var_dump($rs_data);die;
        $tvar = array(
            "title" => "爱心餐列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('products/loveproducts/shopsproduct');
    }


    /**
     * @route('products/boxloveproduct')
     * 添加编辑爱心餐商品
     */
    public function BoxLoveProduct()
    {

        $model = new \app\common\model\Products();
        $typeMdodel = new ProductsType();

        if(request()->isAjax()){

            $param = input();
//            var_dump(234);die;
            $validate = new Product();
            $checkres = $validate->scene('ExchangeProduct')->check($param);
//            var_dump($checkres);die;
            if (!$checkres) {
                $this->error($validate->getError());
            }

            $product_id = $param['product_id'];
            $type_id = $param['type_id'];

            $type = $typeMdodel->where('type_id','=',$type_id)->where('is_del','=',1)->find();

            if(empty($type)){
                $this->error("不存在该分类");
            }
            if($this->_shopid != 0){
                $approved = 1;
            } else {
                $approved = 2;
            }

            $param = [
//
                "product_name" => $param['product_name'],
                "imageurl" => $param['imageurl'],
                "state" => $param['state'],
//                "is_online" => $param['is_online'],
                "sort" => $param['sort'],

                "type_id" => $type_id,
                "admin_id" => $this->_userid,
                "shop_id" => $this->_shopid,
                "approved" => $approved,
                "class_id" => 3,
            ];
//            var_dump($param);die;

            if($product_id == 0){
                $rs = $model->edit($param);
            } else {
                $rs = $model->edit($param,$product_id);
            }
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/products/loveproduct');
        }

        $field = 'type_id,p_id,type_name,sort,add_time';
        $cond['is_del'] = 1;
        $typelist = $typeMdodel->get_tree($field,$cond);
//        echo "<pre>";
//        var_dump($typelist);die;
        $product_id = $_GET['product_id'];
        $product = null;
        $tvar = array(
            "title" => "添加爱心餐商品",
            "time"  => time(),
            "product_id"   => $product_id,
            "typelist" => $typelist,
        );
        if($product_id != 0){
            $tvar["title"] = "编辑爱心餐商品";
            $product = $model->where('product_id','=',$product_id)->find();
        }

        $this->assign("data", $tvar);
        $this->assign("product", $product);
        return $this->fetch('products/loveproducts/boxeproduct');
    }

    /**
     * @route('products/delproduct')
     * 商品删除
     */
    public function UpdateState()
    {
        $productid = input('productid');
        $model = new \app\common\model\Products();
        $where['product_id'] = $productid;
        $where['is_del'] = 1;
        if($this->_shopid != 0){
            $where['shop_id'] = $this->_shopid;
        }
        $product = $model->where($where)->find();

        if(empty($product)){
            $this->error('不存在该商品');
        }
        $param['is_del'] = 2;
        $rs2 = $model->edit($param,$productid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }

    /**
     * @route('products/productstock')
     * 商品库存列表
     */
    public function productstock()
    {
        $get['product_id'] = input('get.product_id');
        $model = new \app\common\model\Products();
        $stockModel = new ProductStock();
        $where['p.product_id'] = $get['product_id'];
        $where['p.is_del'] = 1;
        $isdisplay = true;
        if($this->_shopid != 0){
            $isdisplay = false;
            $where['p.shop_id'] = $this->_shopid;
        }
        $product = $model
            ->alias('p')
            ->leftJoin('ddt_shops s','p.shop_id = s.shop_id')
            ->where($where)
            ->field('p.*,s.shop_name')
//            ->buildSql();
            ->find();
//        var_dump($product->toArray());die;
        if(empty($product)){
            $this->error('不存在该商品','/products/shopsproduct');
        }
        $rs_data = $stockModel->where('product_id','=',$get['product_id'])->where('is_del','=',1)->paginate($this->limit);
//        var_dump(123);die;
        $tvar = array(
            "title" => "商品库存列表",
            "time"  => time(),
            "stock"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign("product", $product->toArray());
        $this->assign('page', $page);
        return $this->fetch('products/productstock');
    }

    /**
     * @route('products/boxestock')
     * 添加编辑商品库存
     */
    public function BoxeStock()
    {
        $model = new ProductStock();
        $pmodel = new \app\common\model\Products();
        $get['stock_id'] = input('get.stock_id');
        $get['product_id'] = input('get.product_id');
        if(input('handle') == "upload"){
            $res = \Upload::FileUpload('file','productimages',1024*1024*20,'jpg,jpeg,png,gif');
            if(!$res['data']){
                $this->result([],-1,$res['msg']);
            }
            $this->result($res['data']['file_path'],1);
        }
        if(request()->isAjax()){
            $stock_id = $this->regpost("stock_id","int",0);
            $product_id = $this->regpost("product_id","int");
            if($stock_id != 0){
                $stock = $model->where('p_s_id','=',$stock_id)->where('is_del','=',1)->find();
                if(!$stock){
                    $this->error('不存在的商品规格','/products/productstock?product_id='.$product_id);
                }
            }
            $where['product_id'] = $product_id;
            $where['is_del'] = 1;
            if($this->_shopid != 0){
                $where['shop_id'] = $this->_shopid;
            }
            $product = $pmodel->where($where)->find();
            if(!$product){
                $this->error('不存在该商品','/products/shopsproduct');
            }
            $getdata = $product->toArray();
            $data['product_id'] = $getdata['product_id'];
//            var_dump(123);die;
            $data['p_s_name'] = $this->regpost("stock_name","string");
            $data['stock'] = $this->regpost("stock_number","string");
//            var_dump($data);die;
            $model->startTrans();
            $rs = $model->edit($data,$stock_id);
            if($rs === false){
                $model->rollback();
                $this->error($model->getError());
            }
            if($this->_shopid != 0) {
                $result = $pmodel->edit(array("approved"=>1),$product_id);
                if($result === false){
                    $model->rollback();
                    $this->error($pmodel->getError());
                }
            }
            $model->commit();
            $this->success('操作成功','/products/productstock?product_id='.$product_id);
        }
        $stock = null;
        if($get['stock_id'] != 0){
            $stock = $model->where('p_s_id','=',$get['stock_id'])->find();
        }
//        var_dump($checkdetail);
        $tvar = array(
            "title" => "添加商品库存",
            "time"  => time(),
            "stock_id" => $get['stock_id'],
            "product_id" => $get['product_id'],
        );
//        var_dump($get['stock_id']);die;
        if($get['stock_id'] != 0){
            $tvar['title'] = "编辑商品库存";
        }
        $this->assign("data", $tvar);
        $this->assign("stock", $stock);
        return $this->fetch('products/boxestock');
    }


    /**
     * @route('products/delstock')
     * 商品库存删除
     */
    public function DelStock()
    {
        $stockid = input('stock_id');
        $model = new ProductStock();
        $where['p_s_id'] = $stockid;
        $where['s.is_del'] = 1;
        if($this->_shopid != 0){
            $where['p.shop_id'] = $this->_shopid;
        }
        $stock = $model
            ->alias('s')
            ->leftJoin('ddt_products p','s.product_id = p.product_id')
            ->where($where)
            ->field('s.*')
            ->find();
//        var_dump($stock);die;
        if(!$stock){
            $this->error('不存在该商品的库存');
        }
        $param['is_del'] = 2;
        $rs2 = $model->edit($param,$stockid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }


    /**
     * @route('products/productimage')
     * 商品图库列表
     */
    public function productimage()
    {
        $get['product_id'] = input('get.product_id');
        $model = new \app\common\model\Products();
        $imageModel = new ProductImage();
        $where['p.product_id'] = $get['product_id'];
        $where['p.is_del'] = 1;
        $isdisplay = true;
        if($this->_shopid != 0){
            $isdisplay = false;
            $where['p.shop_id'] = $this->_shopid;
        }
        $product = $model
            ->alias('p')
            ->leftJoin('ddt_shops s','p.shop_id = s.shop_id')
            ->where($where)
            ->field('p.*,s.shop_name')
//            ->buildSql();
            ->find();
//        var_dump($product->toArray());die;
        if(empty($product)){
            $this->error('不存在该商品','/products/shopsproduct');
        }
        $rs_data = $imageModel->where('product_id','=',$get['product_id'])->order('sort', 'asc')->paginate($this->limit);
//        var_dump(123);die;
        $tvar = array(
            "title" => "商品库存列表",
            "time"  => time(),
            "images"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign("product", $product->toArray());
        $this->assign('page', $page);
        return $this->fetch('products/productimage');
    }

    /**
     * @route('products/showimage')
     * 商品图片显示隐藏
     */
    public function ShowImage()
    {
        $imageid = input('imageid');
        $model = new ProductImage();
        $where['image_id'] = $imageid;
        if($this->_shopid != 0){
            $where['p.shop_id'] = $this->_shopid;
        }
        $image = $model
            ->alias('i')
            ->leftJoin('ddt_products p','i.product_id = p.product_id')
            ->where($where)
            ->field('i.*')
            ->find();
//        var_dump($stock);die;
        if(!$image){
            $this->error('不存在该图片');
        }
        $data = $image->toArray();
        if($data['is_show'] == 0){
            $param['is_show'] = 1;
        } else {
            $param['is_show'] = 0;
        }
        $rs2 = $model->edit($param,$imageid);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }

    /**
     * @route('products/boxeimage')
     * 添加编辑商品图库
     */
    public function BoxeImage()
    {
        $model = new ProductImage();
        $pmodel = new \app\common\model\Products();
        $get['image_id'] = input('get.image_id');
        $get['product_id'] = input('get.product_id');
        if(request()->isAjax()){
            $image_id = $this->regpost("image_id","int",0);
            $product_id = $this->regpost("product_id","int");
            $is_show = input('is_show');
            if($is_show != 0 && $is_show != 1){
                $this->error('参数有误');
            }
            if($image_id != 0){
                $image = $model->where('image_id','=',$image_id)->find();
                if(!$image){
                    $this->error('不存在的商品图片','/products/productimage?product_id='.$product_id);
                }
            }
            $where['product_id'] = $product_id;
            $where['is_del'] = 1;
            if($this->_shopid != 0){
                $where['shop_id'] = $this->_shopid;
            }
            $product = $pmodel->where($where)->find();
            if(!$product){
                $this->error('不存在该商品','/products/shopsproduct');
            }
            $getdata = $product->toArray();
            $data['product_id'] = $getdata['product_id'];
////            var_dump(123);die;
            $data['url'] = $this->regpost("imageurl","string");
            $data['sort'] = $this->regpost("sort","int");
            $data['is_show'] = $is_show;
//            var_dump($data);die;
            $model->startTrans();
            $rs = $model->edit($data,$image_id);
            if($rs === false){
                $model->rollback();
                $this->error($model->getError());
            }
            if($this->_shopid != 0) {
                $result = $pmodel->edit(array("approved"=>1),$product_id);
                if($result === false){
                    $model->rollback();
                    $this->error($pmodel->getError());
                }
            }
            $model->commit();
            $this->success('操作成功','/products/productimage?product_id='.$product_id);
        }
        $image = null;
        if($get['image_id'] != 0){
            $image = $model->where('image_id','=',$get['image_id'])->find();
        }
//        var_dump($checkdetail);
        $tvar = array(
            "title" => "添加商品图片",
            "time"  => time(),
            "image_id" => $get['image_id'],
            "product_id" => $get['product_id'],
        );
//        var_dump($get['stock_id']);die;
        if($get['image_id'] != 0){
            $tvar['title'] = "编辑商品图片";
        }
        $this->assign("data", $tvar);
        $this->assign("images", $image);
        return $this->fetch('products/boxeimage');
    }

	/**
	 * @route('products/productstypelist')
	 * 商品分类列表
	 */
	public function ProductsTypeList(){
		$isdisplay = false;
		// 获取搜索条件
		//总部商铺ID为0，总部可以看到所有商品
		if($this->_shopid != 0){
			//商家身份只能看自己的
			$get['shop_id'] = $this->_shopid;
			//页面的搜索显示
			$isdisplay = true;
		}else{
			//总部的搜索功能
			$get['shop_id'] = input('get.shop_id');
		}
		$get['type_name'] = input('get.type_name');
		$get['is_del'] = 1;
		$field = 'type_id,p_id,type_name,sort,add_time,is_del';
		$order = 'p_id asc,sort asc';

		$Model = new ProductsType();
		$fdata = $Model->get_tree($field,$get,$order);

		$data = [];
		foreach ($fdata as $key => $value){
			if(array_key_exists("type_id", $value)){
				$data[] = [
					'type_id' => $value['type_id'],
					'p_id' => $value['p_id'],
					'type_name' => $value['type_name'],
					'sort' => $value['sort'],
					'add_time' => $value['add_time'],
				];

				if(count($value['children']) > 0){
					foreach ($value['children'] as $k => $v){
						$data[] = [
							'type_id' => $v['type_id'],
							'p_id' => $v['p_id'],
							'type_name' => $v['type_name'],
							'sort' => $v['sort'],
							'add_time' => $v['add_time'],
						];
					}
				}
			}

		}

		$tvar = array(
			"title" => "商品分类列表",
			"time"  => time(),
			"data"  => $data,
			"get"   => $get,
			"isdisplay" => $isdisplay,
		);
//		print_r($tvar);die;
		$this->assign("data", $tvar);
		return $this->fetch('products/productstypelist');
	}


    /**
     * 添加分类
     * @route('products/boxeproducttype')
     */
    public function BoxeProductType(\think\Request $request)
    {
        $id = input('id');
        $ProductType = new ProductsType();

        if(request()->isAjax()){
            $param = $request->param();

            $params['p_id'] = $param['typeid'];
            if(!empty($param['id']) && $param['typeid'] == $param['id']){
                $this->error('操作失败');
            }
            $params['type_name'] = $param['type_name'];
            $params['sort'] = $param['sort'];
            $params['admin_id'] = $this->_userid;

            //  判断当前分类是否有二级分类
            if($param['id'] > 0){
                $cond['p_id'] = $param['id'];
                $type_data = $ProductType->getOne(['p_id' => $param['id']],'type_id');
                if($type_data){
                    $this->error('操作失败,已有子级');
                }
            }

            $rs = $ProductType->edit($params, $param['id']);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功');
        }

        $cond['shop_id'] = 0;
        $cond['p_id'] = 0;
        $type_data = $ProductType->get_list('type_id, type_name', $cond,'sort asc');
//        var_dump($type_data->toArray());die;
        $data = null;
        if($id > 0){
            $data = $ProductType->getOne(['type_id' => $id],'type_id,p_id,type_name,sort');
        }

        $tvar = array(
            "title" => "添加商品分类",
            "time"  => time(),
            'type_data' => $type_data,
            "id" => $id,
            "data" => $data
        );
        $this->assign("data", $tvar);
        return $this->fetch('products/boxeproducttype');
    }

    /**
     * 删除分类
     * @route('products/delproducttype')
     */
    public function delproducttype()
    {
        $id = input('id');

        // 软删除 父级+子级一起删除  （十一）
        $article = ProductsType::get($id,'subs');
        $rs = $article->together('subs')->delete();

        if($rs === false){
            $this->error('操作失败');
        }
        $this->success('操作成功');

    }


    /**
     * @route('products/appraiselist')
     * 商店商品列表
     */
    public function AppraiseList()
    {

        $commentModel = new Comment();
//        判断用户类型
        $isdisplay = true;

        $isshop = $this->_shopid;
        if($isshop != 0){
            $isdisplay = false;
        }
        $get['name'] = input('name','');
        $get['is_show'] = input('is_show','');

        $rs_data = $commentModel->SelectJoinList($get,'a.*,o.order_id,o.order_type,o.shop_id,s.shop_name','comment_id desc',$this->limit);

        $tvar = array(
            "title" => "评论列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('products/appraise/appraiselist');
    }


    /**
     * 编辑消息评论
     * @route('products/boxeappraise')
     */
    public function Boxeappraise()
    {
        $id = input('comment_id');
        $CommentModel = new Comment();

        if(request()->isAjax()){

            $params['content'] = input('content');
            $id = input('comment_id');

            $rs = $CommentModel->edit($params, $id);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功','/products/appraiselist');
        }

        $data = $CommentModel->getOne(['comment_id' => $id],'comment_id,content');


        $tvar = array(
            "title" => "编辑评论",
            "time"  => time(),
            "id" => $id,
            "data" => $data
        );
//        var_dump($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('products/appraise/boxeappraise');
    }

    /**
     * 删除分类
     * @route('products/delappraise')
     */
    public function delappraise()
    {
        $CommentModel = new Comment();
        $id = input('comment_id');

        $is_show = Comment::field('is_show')->get($id)['is_show'];
        $params['is_show'] = 1;
        if($is_show == 1){
            $params['is_show'] = 2;
        }
        // 软删除 父级+子级一起删除  （十一）
        $rs = $CommentModel->edit($params, $id);

        if($rs === false){
            $this->error('操作失败');
        }
        $this->success('操作成功');

    }

}